package com.nassir.excel.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.smodel.io.bean.annotation.ArrayElement;
import org.smodel.io.bean.annotation.Bean;

import com.nassir.excel.cellwriter.ICellWriter;

/**
 * 
 * @author zhangqiulong
 *
 */
@Bean(name = "workbook")
public class WorkBook {
    @ArrayElement(child = "sheet")
    private List<Sheet> sheets;

    @ArrayElement(child = "file")
    private List<File> files;

    @ArrayElement(child = "cellwriter")
    private List<Cellwriter> cellwriters;

    public List<Cellwriter> getCellwriters() {
        return cellwriters;
    }

    public void setCellwriters(List<Cellwriter> cellwriters) {
        this.cellwriters = cellwriters;
    }

    public List<File> getFiles() {
        return files;
    }

    public void setFiles(List<File> files) {
        this.files = files;
    }

    public List<Sheet> getSheets() {
        return sheets;
    }

    public Map<String, ICellWriter<Object>> getCellwriter() {

        Map<String, ICellWriter<Object>> cw = null;
        try {
            cw = new HashMap<String, ICellWriter<Object>>();

            for (Cellwriter cellwriter : cellwriters) {
                @SuppressWarnings("unchecked")
                ICellWriter<Object> iw = (ICellWriter<Object>) Class.forName(cellwriter.getType()).newInstance();
                cw.put(cellwriter.getName(), iw);
            }

            /*if(null != type)
            {
            this.cellWriters.add((ICellWriter) Class.forName(type).newInstance());
            }*/
            return cw;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return cw;
    }

    public void setSheets(List<Sheet> sheets) {
        this.sheets = sheets;
    }

}
